package com.sunrain.utils;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.Properties;

/**
 * Created by adrian on 2015/3/16.
 */
public class SunRainPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    protected String resolvePlaceholder(String placeholder, Properties props) {
        String env_key = StringUtils.trimToEmpty(placeholder);
        String env_value = StringUtils.trimToEmpty(props.getProperty(placeholder));
        //jdbc.jdbcUrl
        if (StringUtils.isNotBlank(env_key) && env_key.equals("jdbc.jdbcUrl")) {
            StringBuffer temp = new StringBuffer();
            temp.append("jdbc:mysql://");
            temp.append(System.getenv("MOPAAS_MYSQL23888_HOST")+":3306/");
            temp.append(System.getenv("MOPAAS_MYSQL23888_NAME"));
            env_value = StringUtils.isNotBlank(temp.toString()) ? temp.toString() : env_value;
        }
        if (StringUtils.isNotBlank(env_key) && env_key.equals("jdbc.user")) {
            String temp = System.getenv("MOPAAS_MYSQL23888_USER");
            env_value = StringUtils.isNotBlank(temp) ? temp : env_value;
        }
        if (StringUtils.isNotBlank(env_key) && env_key.equals("jdbc.password")) {
            String temp = System.getenv("MOPAAS_MYSQL23888_PASSWORD");
            env_value = StringUtils.isNotBlank(temp) ? temp : env_value;
        }
        return env_value;
    }
}
